package com.jiayinlending.jiayindai.activity.mine.mybill;

import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.jiayinlending.jiayindai.R;
import com.jiayinlending.jiayindai.activity.base.BaseActivity;
import com.jiayinlending.jiayindai.adapter.MyBillAdpater;
import com.jiayinlending.jiayindai.net.bean.Info;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MyBillActivity extends BaseActivity {

    @Bind(R.id.recyler_mybill)
    RecyclerView recylerMybill;
    @Bind(R.id.ll_tool)
    LinearLayout llTool;
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        getSubTitle().setText("筛选");
        getToolbarTitle().setText("我的账单");
        initrecyler();
        openTheFilter();
    }

    private void openTheFilter() {
        getSubTitle().setOnClickListener(new View.OnClickListener() {

            private WindowManager.LayoutParams lp;

            @Override
            public void onClick(View v) {
                View view = LayoutInflater.from(MyBillActivity.this).inflate(R.layout.dialog_activity_mybill, null);
                final PopupWindow window = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, true);
                window.setTouchable(true);
                FrameLayout fl_background = (FrameLayout) view.findViewById(R.id.fl_background);
                window.setTouchInterceptor(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        return false;
                        //这里如果返回true的话，touch事件将被拦截
                        //拦截后 PoppWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
                    }
                });
                //（注意一下！！）如果不设置popupWindow的背景，无论是点击外部区域还是Back键都无法弹框
                window.setBackgroundDrawable(new BitmapDrawable());
                fl_background.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        window.dismiss();
                    }
                });
                window.showAsDropDown(llTool);

            }
        });
    }

    private void initrecyler() {
        ArrayList<Info> list2 = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list2.add(new Info());
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recylerMybill.setLayoutManager(layoutManager);
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        recylerMybill.setAdapter(new MyBillAdpater(this, list2));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_bill;
    }
}
